﻿using EasyExcel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;
using Model.Data;
using WssClient.Excel;
using System.IO;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;

namespace WssClient.Item.HengYuan
{
    public class HengYuan : Item<HengYuan, SalesOrder>
    {
        public override HengYuan Init()
        {
            this.title = "恒源";
            this.type = ItemType.Type.HENG_YUAN;
            this.excelName = "hengyuan.xlsx";

            return this;
        }

        public override void Test()
        {
            Dictionary<string, dynamic> dy = new Dictionary<string, dynamic>();

            SalesOrder salesOrder = new SalesOrder();

            salesOrder.orderDate = "2021-03-14";
            salesOrder.customer = "陈基木";
            salesOrder.contactPhone = 15959208866;
            salesOrder.shippingNumber = "CK202103140004";
            salesOrder.shippingDate = "2021-03-16";
            salesOrder.upperCaseAmount = "壹仟肆佰叁拾陆元整";
            salesOrder.amountToal = 1436;

            int i = 0;
            while (++i <= 10)
            {
                Product product1 = new Product();
                product1.idx = i;
                product1.material = "A3";
                product1.sourceSpec = "160×128×18=6";
                product1.weightQuantity = 20.27 + i;
                product1.price = 5.3 + i;
                product1.processingFees = 80 + i;
                product1.amount = 187.43 + i;
                product1.remark = $"三角20件 - {i}";

                salesOrder.products.Add(product1);
            }


            Print(salesOrder);
        }

        public override void Handler(RequestData requestData)
        {
            SalesOrder salesOrder = new SalesOrder();
            salesOrder.Analysis(requestData);
            Print(salesOrder);
        }

        public override void Print(SalesOrder salesOrder)
        {
            ExcelDoc Excel = new ExcelDoc(GVar.item.excelName);

            //string test = dy.abc;
            Excel.SetCell(1, 8, salesOrder.orderDate);
            Excel.SetCell(2, 1, salesOrder.customer);
            Excel.SetCell(2, 4, salesOrder.contactPhone.ToString());
            Excel.SetCell(2, 8, salesOrder.shippingNumber);

            int idx = 4;
            int total = salesOrder.products.Count;
            int nextIdx = total > 0 ? idx : idx + 1;

            for (int i = 0; i < total; i++)
            {
                Product product = salesOrder.products[i];
                Excel.SetCell(nextIdx, 0, product.idx);
                Excel.SetCell(nextIdx, 1, product.material);
                Excel.SetCell(nextIdx, 2, product.sourceSpec);
                Excel.SetCell(nextIdx, 4, CommonHelper.FormatDouble(product.weightQuantity));
                Excel.SetCell(nextIdx, 5, CommonHelper.FormatDouble(product.price));
                Excel.SetCell(nextIdx, 6, CommonHelper.FormatDouble(product.processingFees));
                Excel.SetCell(nextIdx, 7, CommonHelper.FormatDouble(product.amount));
                Excel.SetCell(nextIdx, 9, product.remark);

                nextIdx++;

                if (i < total - 1)
                {
                    Excel.CopyRow(idx, nextIdx);
                }
            }

            Excel.SetCell(nextIdx, 2, salesOrder.upperCaseAmount);
            Excel.SetCell(nextIdx, 7, CommonHelper.FormatDouble(salesOrder.amountToal));

            nextIdx++;

            byte[] bytes = File.ReadAllBytes($"{GVar.CfgPath}/恒源收款码.png");
            int pictureIdx = Excel.Npoi.Sheet.Workbook.AddPicture(bytes, PictureType.PNG);

            var patriarch = Excel.Npoi.Sheet.CreateDrawingPatriarch();
            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 9, nextIdx, 10, nextIdx+1);
            patriarch.CreatePicture(anchor, pictureIdx);

            nextIdx++;

            Excel.SetCell(nextIdx, 6, salesOrder.shippingDate);

            Excel.PrintPreView(false);
        }
    }
}
